package httpCtrler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	st "kjjcgroup_server/model"
	sv "kjjcgroup_server/service"
	"net/http"
	"strconv"
	"time"
)

func PageRouter(router *gin.Engine, routerAuth *gin.RouterGroup) {
	router.GET("/page", pageQuery)
	LogMap["/page"] = "查询下载"
	routerAuth.PUT("/pageAdd", pageAddQuery)
	LogMap["/pageAdd"] = "添加下载"
	routerAuth.PUT("/pageEdit", pageEditQuery)
	LogMap["/pageEdit"] = "编辑下载"
	routerAuth.DELETE("/pageDel", pageDelQuery)
	LogMap["/pageDel"] = "删除下载"
}

func pageQuery(c *gin.Context) {
	PageId := c.Query("PageId")
	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)

	var collArr []*st.PageMod
	if PageId != "" {
		err = mysql.Where("page_id=?", PageId).Find(&collArr)
		_, _ = mysql.Where("page_id=?", PageId).Update(&st.PageMod{ClicksNumber: collArr[0].ClicksNumber + 1})
	} else {
		err = mysql.Find(&collArr)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
}

func pageAddQuery(c *gin.Context) {
	resDt := &st.PageMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	//	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	mysql.ShowSQL(true)
	UserAccount := CurrentAccount(c)

	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	fmt.Println(t)
	_, err = sess.Cols("page_name", "page_img", "page_info", "page_note", "user_account", "clicks_number", "create_time", "update_time").InsertOne(&st.PageMod{PageName: resDt.PageName, PageImg: resDt.PageImg, PageInfo: resDt.PageInfo, PageNote: resDt.PageNote, UserAccount: UserAccount, ClicksNumber: resDt.ClicksNumber, CreateTime: t, UpdateTime: t})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "添加" + resDt.PageName + "成功!"})
}

func pageEditQuery(c *gin.Context) {
	resDt := &st.PageMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	// mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	_, err = sess.Where("page_id=?", resDt.PageId).Update(&st.PageMod{PageName: resDt.PageName, PageImg: resDt.PageImg, PageInfo: resDt.PageInfo, PageNote: resDt.PageNote, ClicksNumber: resDt.ClicksNumber})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + strconv.Itoa(resDt.PageId) + " 编辑成功!"})
}

func pageDelQuery(c *gin.Context) {
	PageId := c.Query("PageId")
	mysql, err := sv.GetEngBom()
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	mysql.ShowSQL(true)
	_, err = mysql.Where("page_id=?", PageId).Delete(&st.PageMod{})
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + PageId + " 删除成功!"})
}
